package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC349两个数组的交集 {

    public int[] intersection(int[] nums1, int[] nums2) {

        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        HashSet<Integer> set2 = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])){
                set2.add(nums2[i]);
            }
        }

        int[] res = new int[set2.size()];
        int i = 0;
        for (Integer j : set2) {
            res[i++] = j;
        }

        return res;
    }

    @Test
    public void test() {
        // 测试代码
        intersection(new int[]{1,2,2,1},new int[]{2,2});
        intersection(new int[]{4,9,5},new int[]{9,4,9,8,4});

    }

}
